home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / 3dlib30b.zip / CTM3D.H < prev    next >
C/C++ Source or Header  |  1994-03-10  |  9KB  |  161 lines

  1. /******************************************************************************
  2. *                                    ctm3d                                    *
  3. *                                                                             *
  4. *                          Homogeneous Coordinates                            *
  5. *                          -----------------------                            *
  6. *                                                                             *
  7. *      Homogeneous coordinates allow transformations to be represented by     *
  8. *      matrices. A 3x3 matrix is used for 2D transformations, and a 4x4 matrix*
  9. *      for 3D transformations.                                                *
  10. *                                                                             *
  11. *      THIS MODULE IMPLEMENTS ONLY 3D TRANSFORMATIONS.                        *
  12. *                                                                             *
  13. *      in homogeneous coordination the point P(x,y,z) is represented as       *
  14. *      P(w*x, w*y, w*z, w) for any scale factor w!=0.                         *
  15. *      in this module w == 1.                                                 *
  16. *                                                                             *
  17. * Transformations:                                                            *
  18. *          1. translation                                                     *
  19. *                  [x, y, z] --> [x + Dx, y + Dy, z + Dz]                     *
  20. *                                                                             *
  21. *                              ┌          ┐                                   *
  22. *                              │1  0  0  0│                                   *
  23. *              T(Dx, Dy, Dz) = │0  1  0  0│                                   *
  24. *                              │0  0  1  0│                                   *
  25. *                              │Dx Dy Dz 1│                                   *
  26. *                              └          ┘                                   *
  27. *          2. scaling                                                         *
  28. *                  [x, y, z] --> [Sx * x, Sy * y, Sz * z]                     *
  29. *                                                                             *
  30. *                          ┌          ┐                                       *
  31. *                          │Sx 0  0  0│                                       *
  32. *              S(Sx, Sy) = │0  Sy 0  0│                                       *
  33. *                          │0  0  Sz 0│                                       *
  34. *                          │0  0  0  1│                                       *
  35. *                          └          ┘                                       *
  36. *                                                                             *
  37. *          3. rotation                                                        *
  38. *                                                                             *
  39. *              a) Around the Z axis:                                          *
  40. *                                                                             *
  41. *                  [x, y, z] --> [x*cost - t*sint, x*sint + y*cost, z]        *
  42. *                      ┌                  ┐                                   *
  43. *                      │cost  sint   0   0│                                   *
  44. *              Rz(t) = │-sint cost   0   0│                                   *
  45. *                      │0     0      1   0│                                   *
  46. *                      │0     0      0   1│                                   *
  47. *                      └                  ┘                                   *
  48. *                                                                             *
  49. *              b) Around the X axis:                                          *
  50. *                                                                             *
  51. *                  [x, y, z] --> [x, y*cost - z*sint, y*sint + z*cost]        *
  52. *                      ┌                  ┐                                   *
  53. *                      │1     0     0    0│                                   *
  54. *              Rx(t) = │0     cost  sint 0│                                   *
  55. *                      │0    -sint  cost 0│                                   *
  56. *                      │0     0     0    1│                                   *
  57. *                      └                  ┘                                   *
  58. *                                                                             *
  59. *              c) Around the Y axis:                                          *
  60. *                                                                             *
  61. *                  [x, y, z] --> [xcost + z*sint, y, z*cost - x*sint]         *
  62. *                      ┌                  ┐                                   *
  63. *                      │cost  0   -sint  0│                                   *
  64. *              Ry(t) = │0     1    0     0│                                   *
  65. *                      │sint  0    cost  0│                                   *
  66. *                      │0     0    0     1│                                   *
  67. *                      └                  ┘                                   *
  68. *                                                                             *
  69. *   transformation of the vector [x,y,z,1] by transformation matrix T is given *
  70. *   by the formula:                                                           *
  71. *                                         ┌   ┐                               *
  72. *              [x', y', z', 1] = [x,y,z,1]│ T │                               *
  73. *                                         └   ┘                               *
  74. * Optimizations:                                                              *
  75. *   The most general composition of R, S and T operations will produce a matrix*
  76. *   of the form:                                                              *
  77. *              ┌                       ┐                                      *
  78. *              │r11    r12     r13    0│                                      *
  79. *              │r21    r22     r23    0│                                      *
  80. *              │r31    r32     r33    0│                                      *
  81. *              │tx     ty      tz     1│                                      *
  82. *              └                       ┘                                      *
  83. *   The task of matrix multiplication can be simplified by                    *
  84. *      x' = x*r11 + y*r21 + z*r31 + tx                                        *
  85. *      y' = x*r12 + y*r22 + z*r32 + ty                                        *
  86. *      z' = x*r13 + y*r23 + z*r33 + tz                                        *
  87. *                                                                             *
  88. *                                                                             *
  89. * See also:                                                                   *
  90. *      "Fundamentals of Interactive Computer Graphics" J.D FOLEY A.VAN DAM    *
  91. *      Adison-Weslely ISBN 0-201-14468-9 pp 245-265                           *
  92. *                                                                             *
  93. ******************************************************************************/
  94.  
  95. #ifndef _ctm3d_h
  96. #define _ctm3d_h
  97.  
  98. #if !defined(_hdr3d_h)
  99. #include "hdr3d.h"
  100. #endif
  101.  
  102. class ctm {
  103.  
  104. public: 
  105.  
  106.    double r11, r12, r13;
  107.    double r21, r22, r23;
  108.    double r31, r32, r33;
  109.    double tx,  ty,  tz ;
  110.  
  111.    ctm(); // same as constructor setunit in pascal 
  112.    ctm(ctm& src); // same as copy constructor in pascal
  113.  
  114.    void copy(ctm& src);
  115.    
  116.    void setUnit();
  117.    void save(ctm& dest);
  118.  
  119.    void translate(double Dx, double Dy, double Dz);
  120.    void translateX(double Dx);
  121.    void translateY(double Dy);
  122.    void translateZ(double Dz);
  123.  
  124.    void rotateX(double t);
  125.    void rotateY(double t);
  126.    void rotateZ(double t);
  127.  
  128.    void scale(double Sx, double Sy, double Sz);
  129.    void scaleX(double Sx);
  130.    void scaleY(double Sy);
  131.    void scaleZ(double Sz);
  132.  
  133.    void Left_translate(double Dx, double Dy, double Dz);
  134.  
  135.    void Left_translateX(double Dx);
  136.    void Left_translateY(double Dy);
  137.    void Left_translateZ(double Dz);
  138.  
  139.    void Left_rotateX(double t);
  140.    void Left_rotateY(double t);
  141.    void Left_rotateZ(double t);
  142.  
  143.    void Left_scale(double Sx, double Sy, double Sz);
  144.  
  145.    void Left_scaleX(double Sx);
  146.    void Left_scaleY(double Sy);
  147.    void Left_scaleZ(double Sz);
  148.  
  149.    void transform(point3d& t, point3d p);
  150.  
  151.    void inv_transform(point3d& p); // transform the input
  152.  
  153.    void inverse(); // M ^-1 : Bring the matrix to -1 power
  154.    void multiply(const ctm c); // multiply from right self * c
  155.    void multiply_2(ctm& a, ctm& b);
  156.    
  157. }; // ctm object definition
  158.  
  159. typedef ctm *ctmPtr;
  160. #endif
  161.